const app = getApp();
const publicFun = require('/utils/public.js');
import { getClimateActionTab } from '/utils/request'
Page({
  data: {
    currentIndex: 0
  },
  onLoad() {
    this.getClimateActionTabList();
    my.removeStorage({ key: 'byoimgurl' });
  },
  uploadTap(e) {
    let data = e.currentTarget.dataset;
    my.navigateTo({
      url: '/pages/upload/upload?actionid='+data.actionid
    })
  },
  actionRulesClick() {
    my.navigateTo({
      url: '/pages/actionRules/actionRules'
    })
  },
  cardTotalTap(e) {
    let data = e.currentTarget.dataset;
    my.navigateTo({
      url: '/pages/cardTotalList/cardTotalList?id='+data.id
    })
  },
  toggleSwitch(e) {
    let data = e.currentTarget.dataset;
    let num = 0;
    if(data.list.AL.length % 2 == 0){
      num = data.list.AL.length;
    }else{
      num = data.list.AL.length+1;
    }
    // 少于4条数据就不需要展开了
    // if (data.list.AL.length < 3) return;
    let animation = my.createAnimation({
      duration: 1000,
    });
    if(data.list.isExpand){
      this.data.tabList[data.index].boxHeight = 316 / 2;
    }else{
      // 展开的高度是动态计算的，用一行的高度（50）去乘以数组的数量
      let height = num * 152 / 2+ 'rpx'; 
      this.data.tabList[data.index].boxHeight = height;
    }
    this.data.tabList[data.index].isExpand = !data.list.isExpand;
    this.setData({
      tabList: this.data.tabList,
    });
  },
  async getClimateActionTabList() { 
    try{
      var params = {
        keyword: ''
      }
      const res = await getClimateActionTab(params)
      if(res.S == 1){
        let arr = [];
        let reg=/\[\[\[|\]\]\]/g;
        let data = res;
        data.L.forEach(item => {
          item.isExpand = false;
          item.boxHeight = 316 / 2;
          item.Des = item.Des.replace(reg,'');
          arr.push(item)
        })
        this.setData({ 
          tabList: arr,
          linkQianZhui: res.QianZhui,
          total: res.Total,
          totalUnit: res.TotalUnit
        })
      }else{
        publicFun.showToast(res.M ? res.M : '加载失败');
      }
    }catch(e){
      publicFun.showToast('失败，请稍后再试')
    }
  },
});
